package com.besovideo.mpu;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;

import com.smarteye.mpu.MPUGlobal;
import com.smarteye.mpu.MainService.Connection;
import com.smarteye.mpu.coresdk.MPUKey;

public class RateSetDialog extends Dialog {
	
	Context context;
	int lay;
	private Connection mConnection;
	
	public RateSetDialog(Context context) {
		super(context, R.style.NotTransparentDialogHaveTitle);
		this.context = context;
		this.lay = R.layout.normal_seek_bar;
		mConnection = ((MPUGlobal) ((MainActivity) context).getApplication()).getConnection();
	}

	TextView now;
	TextView end;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(lay);
		
		SeekBar seekBar = (SeekBar) findViewById(R.id.seekBar);
		seekBar.setMax(2000 * 1000);

		now = (TextView)findViewById(R.id.seekBarBegin);
		end = (TextView)findViewById(R.id.seekBarEnd);
		end.setText("2000 * 1000");

		int progress = mConnection.getPreInt(MPUKey.MPU_I_REALTIME_VIDEO_BITRATE);
		if (progress != 0) {
			seekBar.setProgress(progress);
			mConnection.setSDKOptionInt(MPUKey.MPU_I_REALTIME_VIDEO_BITRATE, progress);
		} else {
			seekBar.setProgress(mConnection.getSDKOptionInt(MPUKey.MPU_I_REALTIME_VIDEO_BITRATE));
		}
		now.setText(String.valueOf(seekBar.getProgress()));

		seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

			@Override
			public void onStopTrackingTouch(SeekBar arg0) {
				mConnection.setSDKOptionInt(
						MPUKey.MPU_I_REALTIME_VIDEO_BITRATE,
						arg0.getProgress());
				dismiss();
			}

			@Override
			public void onStartTrackingTouch(SeekBar arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onProgressChanged(SeekBar arg0, int arg1,
					boolean arg2) {
				// TODO Auto-generated method stub
				now.setText(String.valueOf(arg1));
			}
		});
	}
}